//
//  QDStationTagsHeaderView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/3.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

protocol QDStationTagsHeaderViewDelegate: class {
    
    /// 点击事件
    func stationTagsHeaderView(view: QDStationTagsHeaderView, clickAt index:Int)
}


extension QDStationTagsHeaderViewDelegate {
    func stationTagsHeaderView(view: QDStationTagsHeaderView, clickAt index:Int) {}
}

/// 地铁详情消息tag
final class QDStationTagsHeaderView: UIView {
    
    /// 标题数组
    var titles = ["出入口信息","公交信息","基础设施","生活服务"]
    /// 点击响应
    weak var delegate: QDStationTagsHeaderViewDelegate?
    
    /// 内容视图
    private let contentView = UIView()
    /// 选中的视图
    private weak var selectButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 装载视图
    private func setupViews(){
        backgroundColor = .white
        
        contentView.backgroundColor = .white
        contentView.clipsToBounds = true
        contentView.layer.borderColor = QDColors.theme.cgColor
        contentView.layer.borderWidth = 1
        contentView.layer.cornerRadius = 4
        
        addSubview(contentView)
        
        //装载默认数据
        for i in 0..<titles.count {
            
            let button = UIButton()
            button.tag = i
            button.isSelected = i == 0
            button.setTitle(titles[i], for: .normal)
            button.titleLabel?.font = Font.regular.font(size: 13)
            button.setTitleColor(.white, for: .selected)
            button.setTitleColor(QDColors.theme, for: .normal)
            button.setBackgroundImage(UIColor.white.ritl_image(), for: .normal)
            button.setBackgroundImage(QDColors.theme.ritl_image(), for: .selected)
            if i == 0 {
                selectButton = button
            }
            button.addTarget(self, action: #selector(buttonDidTap(sender:)), for: .touchUpInside)
            contentView.addSubview(button)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        contentView.ritl_width = ritl_width * 0.9
        contentView.ritl_height = 30
        contentView.ritl_centerX = ritl_width / 2.0
        contentView.ritl_centerY = ritl_height / 2.0
        
        let width = contentView.ritl_width / CGFloat(contentView.subviews.count)
        for (index,button) in contentView.subviews.enumerated() {
            button.ritl_originX = width * CGFloat(index)
            button.ritl_height = contentView.ritl_height
            button.ritl_width = width
            button.ritl_originY = 0
        }
    }
    
    /// 按钮点击
    @objc func buttonDidTap(sender: UIButton) {
        
        selectButton?.isSelected = false
        sender.isSelected = true
        selectButton = sender
        
        delegate?.stationTagsHeaderView(view: self, clickAt: sender.tag)
    }
}


